package action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_news.EntityBeanOc_News;
import form.Insert_NewsForm;
import form.NewsListForm;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.Convert;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_News;

public class Action_Oc_News extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;

	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}

	public ActionForward admin_init_news(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("初始化新闻模块");
		// 封装新闻查询的数据及表单
		NewsListForm newslistform = (NewsListForm) form;
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_NEWS where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_News> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_load_news(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		System.out.println("ID=" + id);
		EntityBeanOc_News e = this.service_admin.Find_Oc_News_By_Id(id);
		System.out.println("初始化添加一个新闻");
		// 封装新闻查询的数据及表单
		String rootPath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		System.out.println(e.getId());
		Convert c = new Convert();
		e.setNews_clob_string(c.ConvertClobToString(e.getNews_clob()));
		request.setAttribute("e", e);
		// response.sendRedirect(rootPath +
		// "/kindedit/jsp/ocean_demo_update_news.jsp?title="+e.getNews_title());
		return mapping.findForward("success");
	}

	public ActionForward admin_add_news(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("初始化添加一个新闻");
		// 封装新闻查询的数据及表单
		String rootPath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		// response.sendRedirect(rootPath +
		// "/kindedit/jsp/ocean_demo_add_news.jsp");

		// return null;
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_news(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Insert_NewsForm f = (Insert_NewsForm) form;
		String news_clobstring = f.getNews_clob();
		String news_clobstring1 = request.getParameter("news_clob");
		String news_title = f.getNews_title();
		System.out.println("写入一个新闻信息");
		System.out.println("news_title=" + news_title);
		System.out.println("news_clobstring=" + news_clobstring);
		System.out.println("news_clobstring1=" + news_clobstring1);
		// news_clobstring="news_clobstringnews_clobstringnews_clobstringnews_clobstring";
		// 初始化一个新闻包信息
		EntityBeanOc_News e = new EntityBeanOc_News();
		EntityBeanL_Sequence l = this.service_public
				.Find_L_Sequence_By_seq_Name("SEQ_OC_NEWS");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		e.setId(id);
		e.setDel("OFF");
		e.setNews_clob_string(news_clobstring);
		e.setNews_title(news_title);
		e.setUpdate_time(new Date());
		e.setUser_id(1);
		this.service_admin.Insert_Oc_News(e);
		this.service_public.Update_L_Sequence(l);

		NewsListForm newslistform = new NewsListForm();
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_NEWS where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_News> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_delete_news(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		NewsListForm f = (NewsListForm) form;
		String id = f.getId();
		
		System.out.println("id=" + id);
		id = this.cut_tail(id);
		this.service_admin.Delete_Oc_News(id);

		System.out.println("newslistform.getCurrentpage()="
				+ f.getCurrentpage());
		System.out.println("newslistform.getNews_title()=" + f.getNews_title());
		System.out.println("newslistform.getOver_time()=" + f.getOver_time());
		System.out.println("newslistform.getStart_time()=" + f.getStart_time());
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_NEWS where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(f.getRowcountperpage()), Integer.valueOf(f
						.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_News> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("newslistform", f);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_update_news(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("更新一个数据");
		Insert_NewsForm insert_newsform=(Insert_NewsForm)form;
		String id=insert_newsform.getId();
		String title=insert_newsform.getNews_title();
		String clob=insert_newsform.getNews_clob();
		EntityBeanOc_News e=this.service_admin.Find_Oc_News_By_Id(id);
		e.setNews_clob_string(clob);
		e.setUpdate_time(new Date());
		e.setNews_title(title);
		this.service_admin.Delete_Oc_News(e);
		this.service_admin.Insert_Oc_News(e);
		NewsListForm newslistform = new NewsListForm();
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_NEWS where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_News> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
		
		
		
		
	}
	public ActionForward admin_search_news(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception  
	{
		System.out.println("初始化查询新闻模块");
		//封装新闻查询的数据及表单
		NewsListForm newslistform=(NewsListForm)form;
		System.out.println("newslistform.getCurrentpage()="+newslistform.getCurrentpage());
		System.out.println("newslistform.getNews_title()="+newslistform.getNews_title());
		System.out.println("newslistform.getOver_time()="+newslistform.getOver_time());
		System.out.println("newslistform.getStart_time()="+newslistform.getStart_time());
		String sql="select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_NEWS where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus=new PageBeanPlus("oracle", "sql",sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer.valueOf(newslistform.getCurrentpage()),"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist=new InterfaceForResultSetToList();
		List<ViewOc_News>list=interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());
		
		request.setAttribute("pagebeanplus", pagebeanplus);//设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()"+list.size());
		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
